package genalgoritm.factories;

import genalgoritm.parametrs.ParametrizedComposite;

import java.lang.reflect.InvocationTargetException;

import org.apache.commons.math.genetics.MutationPolicy;
import org.eclipse.swt.widgets.Composite;

public class MutationFactory {
	private String name;
	private Class<?> policy;
	private Class<?> composite;
	private String description;

	public String getName() {
		return name;
	}

	public MutationPolicy getPolicy(Object... params) {
		try {
			Class<?>[] classes = new Class[params.length];
			for (int i = 0; i < classes.length; i++) {
				classes[i] = params[i].getClass();
			}
			return (MutationPolicy) policy.getConstructor(classes).newInstance(params);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
		return null;
	}

	public ParametrizedComposite getComposite(Composite parent, int style) {
		try {
			if (composite == null) return null;
			return (ParametrizedComposite) composite.getConstructors()[0].newInstance(parent, style);
		}catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return null;
	}

	public String getDescription() {
		return description;
	}

	public MutationFactory(String name, Class<?> policy, Class<?> composite, String description) {
		this.name = name;
		this.description = description;
		this.policy = policy;
		this.composite = composite;
	}

}
